package com.liu.demo1.testDemo;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * @author liusiyu
 * @ClassName AppleMovies
 * @date 2021年12月27日 09:36
 */
public class AppleMovies {
    public static void main(String[] args) throws IOException {
        String url = "http://a1.phobos.apple.com/us/r1000/000/Features/atv/AutumnResources/videos/entries.json";
        String json = getUrlContent(url);
        System.out.println(json);
        resolveJson(json);
    }

    public static String getUrlContent(String urlStr) throws IOException {

        URL url = null;

        HttpURLConnection httpURLConnection = null;

        BufferedReader in = null;

        String inputLine = null;

        StringBuffer json = new StringBuffer();

        try {
            url = new URL(urlStr);
            httpURLConnection = (HttpURLConnection) url.openConnection();
            in = new BufferedReader((new InputStreamReader(httpURLConnection.getInputStream(),"UTF-8")));
            while ((inputLine = in.readLine()) != null){
                json.append(inputLine);
            }
        }catch (IOException e){
            System.out.println("出错了");
        }finally {
            if (in != null){
                in.close();
            }
        }

        return json.toString();

    }

    public static boolean resolveJson(String json){

        try {
            JSONArray jsonArray = new JSONArray(json);
            for (int i = 0 ; i < jsonArray.length(); i++){
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                String id = jsonObject.getString("id");
                JSONArray jsonArray1 = jsonObject.getJSONArray("assets");
                String filePath = "";
                for (int j = 0; j < jsonArray1.length(); j++){
                    filePath = "/Users/liusiyu/JavaFiles/appleMovie/"+id;
                    File file = new File(filePath);
                    if (!file.exists()){
                        file.mkdirs();
                        System.out.println(filePath+"创建成功...");
                    }
                    JSONObject jsonObject1 = jsonArray1.getJSONObject(j);
                    String url = jsonObject1.getString("url");
                    String zId = jsonObject1.getString("id");
                    System.out.println(zId+"开始下载...");
                    downLoad(url,zId,filePath);
                    System.out.println(zId+"下载完毕...");
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    public static void downLoad(String urlStr, String fileName, String filePath){
        URL url = null;
        HttpURLConnection conn = null;
        InputStream in = null;
        FileOutputStream fos = null;
        try {
            url = new URL(urlStr);
            conn = (HttpURLConnection) url.openConnection();
            in = conn.getInputStream();
            //缓冲
            if (!filePath.endsWith("/")){
                filePath = filePath + "/";
            }
            File file = new File(filePath+fileName+".mov");
            fos = new FileOutputStream(file);
            byte [] bytes = new byte[8*1024];
            int length = 0;
            while ((length = in.read(bytes)) != -1){
                fos.write(bytes,0,length);
            }
        }catch (IOException e){
            System.out.println("");
        }finally {
            try {
                fos.close();
                in.close();
                conn.disconnect();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
